package com.atbeijing.bigdata.spark.core.acc

import org.apache.spark.{SparkConf, SparkContext}

object Spark01_Var_Acc {

    def main(args: Array[String]): Unit = {
        val conf = new SparkConf().setMaster("local").setAppName("Acc")
        val sc = new SparkContext(conf)
        val rdd = sc.makeRDD(List(1,2,3,4),2)
        var sum = 0
        //算子内使用算子外变量,闭包效果,会将sum传到executor端
        rdd.foreach(
            num => {
                println("sum = " + sum )
                sum = sum + num
            })
        println(sum) //0
        sc.stop()
    }
}
